<?php

declare (strict_types=1);

namespace App\Erp\Model;

use App\System\Service\SystemUserService;
use Hyperf\Database\Model\SoftDeletes;
use Mine\MineModel;

/**
 * @property int $id
 * @property string $name 仓库名称
 * @property string $address 仓库地址
 * @property string $storage_fee 仓储费
 * @property string $carry_fee 搬运费
 * @property int $user_id 负责人
 * @property int $status 状态 (1正常 0停用)
 * @property int $sort 排序
 * @property int $mer_id 商户id
 * @property int $created_by 创建者
 * @property int $updated_by 更新者
 * @property \Carbon\Carbon $created_at 创建时间
 * @property \Carbon\Carbon $updated_at 更新时间
 * @property string $deleted_at 删除时间
 */
class ErpStorehouse extends MineModel
{
    use SoftDeletes;

    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 'erp_storehouse';
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'id', 'name', 'address', 'storage_fee', 'carry_fee', 'user_id', 'user_name', 'status', 'sort',
        'mer_id', 'created_by', 'updated_by', 'created_at', 'updated_at', 'deleted_at'
    ];
    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = [
        'id' => 'integer', 'storage_fee' => 'decimal:2', 'carry_fee' => 'decimal:2',
        'user_id' => 'array', 'user_name' => 'string', 'status' => 'string', 'sort' => 'integer', 'mer_id' => 'integer',
        'created_by' => 'integer', 'updated_by' => 'integer', 'created_at' => 'datetime',
        'updated_at' => 'datetime'
    ];

    public function setUserIdAttribute($value)
    {
        /** @var SystemUserService $userService */
        $userService = make(SystemUserService::class);
        $this->attributes['user_name'] = $userService->read((int)$value[0])['username'] ?? '';
        $this->attributes['user_id'] = json_encode($value, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
    }
}